package cn.itcast.xuesheng.web.servlet;

import cn.itcast.xuesheng.utils.GetIputil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigRequest;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import javax.servlet.http.HttpServletRequest;

/**
 *
 */

public class TestDemo {

        //ACCESS_KEY、ACCESS_SECRET请替换成您的阿里云accesskey和secret

        private static String ACCESS_KEY = "LTAI4FkZmvNLTd3idWtbhZZQ";
        private static String ACCESS_SECRET = "Kov7o4R66Qz9mmgI5qRETQmlY1NldX";

        /**
         * sessionId
         *
//         * @param sessionId 为h5从风控服务器获取的返回值(字符串)，具体格式：sessionId<-->sig<-->token<-->scene
         * @param req
         * @return
         */
        public static boolean check(HttpServletRequest req) {
            try {
//                if (StringUtils.isEmpty(sessionId)) {
//                    return false;
//                }
                IClientProfile profile = DefaultProfile.getProfile("cn-beijing", ACCESS_KEY, ACCESS_SECRET);
                IAcsClient client = new DefaultAcsClient(profile);
                DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", "afs", "afs.aliyuncs.cn");
                AuthenticateSigRequest request = new AuthenticateSigRequest();
                String sessionId = req.getParameter("data.sessionId");
                String token = req.getParameter("NVC_Opt.token");
                String sig = req.getParameter("data.sig");
                request.setSessionId(sessionId);// 必填参数，从前端获取，不可更改
                request.setSig(sig);// 必填参数，从前端获取，不可更改
                request.setToken(token);// 必填参数，从前端获取，不可更改
                request.setScene("ic_login");// 必填参数，从前端获取，不可更改
                request.setAppKey("FFFF0N00000000008D54");// 必填参数，此处填阿里云控制台给你的appkey
                request.setRemoteIp(GetIputil.getIp(req));// 必填参数，后端填写
                System.out.println(sessionId);
                System.out.println(token);
                System.out.println(sig);
                AuthenticateSigResponse response = client.getAcsResponse(request);
                if (response.getCode() == 100) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    }